//
//  STCardMangerController.m
//  KunLun
//
//  Created by Mac on 2017/11/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STCardMangerController.h"
#import "STBankModel.h"
#import "STBacnkCardTableViewCell.h"
#import "STAddCardViewController.h"
@interface STCardMangerController ()
@property(nonatomic, strong) STNoresultView                                    *noResultView;
@property(nonatomic, strong) NSMutableArray<STBankModel*>                    *dataSouce;
@property(nonatomic, strong) STButton                                           *addButton;
@end

@implementation STCardMangerController

#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"银行卡";
    [self configSubView];
    self.dataSouce = [NSMutableArray new];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    _addButton = [[STButton alloc] initWithFrame:CGRectMake(10, 0, UIScreenWidth - 20, 55)
                                           title:@"+添加银行卡"
                                      titleColor:FirstTextColor
                                       titleFont:18
                                    cornerRadius:5
                                 backgroundColor:[UIColor whiteColor]
                                 backgroundImage:nil
                                           image:nil];
    [_addButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddButton];
    }];
    _addButton.st_centerY = 50;
    _addButton.hidden = YES;
    _addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:_addButton];
    self.tableView.tableFooterView = footer;
    
    //无数据vieww
    self.noResultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350)
                                                        title:@"暂无银行卡"
                                                  buttonTitle:@"添加银行卡"
                                                 buttonHandle:^(NSString *titleString) {
        [weakSelf onSelectedAddButton];
    }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendfetchBanksRequest];
    }];
    
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [STBacnkCardTableViewCell CellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    STBacnkCardTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[STBacnkCardTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.section];
    __weak typeof(self) weakSelf =  self;
    [cell.defultButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedDefultButtonWithIndexPath:indexPath];
    }];
    [cell.delButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedDelButtonWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (void)reloadData{
    self.addButton.hidden = !self.dataSouce.count;
    self.noResultView.hidden = self.dataSouce.count;
    [self.tableView reloadData];
}
#pragma mark --Action Method
- (void)onSelectedAddButton{
    [self.navigationController pushViewController:[STAddCardViewController new] animated:YES];
}
- (void)onSelectedDefultButtonWithIndexPath:(NSIndexPath*)indexPath{
    STBankModel * model = self.dataSouce[indexPath.section];
    [self sendDefultRequestWithModel:model];
}
- (void)onSelectedDelButtonWithIndexPath:(NSIndexPath*)indexPath{
    STBankModel * model = self.dataSouce[indexPath.section];
    [self showAlertCancelAndConfirm:@"确定要删除该银行卡吗？" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
            [self sendDelRequestWithModel:model];
        }
    }];
}
#pragma mark --NetWork Method
- (void)sendfetchBanksRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/bankcard?token=",[STUserManger defult].token];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_header endRefreshing];
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * dataArray = dataDic[@"list"];
                                                     self.dataSouce = [NSMutableArray new];
                                                     for (NSDictionary* dataDic in dataArray) {
                                                         STBankModel * model = [STBankModel mj_objectWithKeyValues:dataDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                     [self reloadData];
                                                 }
     
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self reloadData];
                                                 [SVProgressHUD dismiss];
                                                 // [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendDefultRequestWithModel:(STBankModel*)model{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
//    if (model) {
//        [dic setObject:model.bankID forKey:@"addr_id"];
//    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/edit_address"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"更改成功"];
                                                 //model.isdefault = !model.isdefault;
                                                 [self.tableView reloadData];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendDelRequestWithModel:(STBankModel*)model{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
//    if (model) {
//        [dic setObject:model.bankID forKey:@"addr_id"];
//    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/cash/del_address"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                                 [self.tableView.mj_header beginRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end

